<?php

class home {

    function home() {
        
    }

    function getCategory($parent_id=0) {
        global $db;
        $sql = "SELECT * FROM article_category WHERE parent_id=" . intval($parent_id);
        $rs = $db->query($sql);
        return $rs;
    }

    function getNewestArticleByCategoryId($categoryId) {
        global $db;
        $sql = "SELECT id,title,link,short_description,image FROM article WHERE cat_id=" . intval($categoryId) . " AND status='yes' ORDER BY create_date DESC";
        $rs = $db->query_first($sql);
        return $rs;
    }

    function highestView($limit=15) {
        global $db;
        $sql = "SELECT title,link FROM article WHERE status='yes' ORDER BY view_count DESC limit $limit";
        $rs = $db->query($sql);
        $totalRows = $db->num_rows($rs);
        return array(
            "rs" => $rs,
            "totalRows" => $totalRows
        );
    }

    function get_cat_id_to_arr($parent_id = 0) {
        global $db;
        if (!$trees)
            $trees = array();
        $rsCat = $db->query("SELECT * FROM article_category WHERE  parent_id = " . intval($parent_id));
        if ($rsCat) {
            while ($rs = $db->fetch_array($rsCat)) {
                $trees[] = $rs['id'];
            }
        }
        return $trees;
    }

    function getLastestArticleFrontPage() {
        global $db;
        $sql = "SELECT title,image,short_description,link FROM article WHERE front_page='yes' AND status='yes' ORDER BY front_page_date DESC LIMIT 1";
        $rs = $db->query_first($sql);
        return $rs;
    }

    function getArticleFrontPage($limit=10) {
        global $db;
        $sql = "SELECT title,link,image FROM article WHERE front_page='yes' AND status='yes' ORDER BY front_page_date DESC LIMIT 1,$limit";
        $rs = $db->query($sql);
        $totalRows = $db->num_rows($rs);
        return array(
            "rs" => $rs,
            "totalRows" => $totalRows
        );
    }

    function getHomeArticle() {
        global $db,$config;
        $sql = "SELECT count(id) as numrows FROM article WHERE status='yes'";
        $rsTotalRows = $db->query_first($sql);
        
        $totalRows = intval($rsTotalRows['numrows']);
        $totalPages = ceil($totalRows / $config ['total_article_on_category']);
        $start = (($config ['page'] * $config ['total_article_on_category']) - $config ['total_article_on_category']);
        $limit = $config ['total_article_on_category'];

        $sql2 = "SELECT * FROM article WHERE status='yes' ORDER BY create_date DESC LIMIT $start,$limit";
        $rsArticles = $db->query($sql2);
        $result = array();
        while ($row = $db->fetch_array($rsArticles)) {
            $result[$row['id']] = $row;
        }
        return array(
            "totalRows" => $totalRows,
            "totalPages" => $totalPages,
            "arrRs" => $result
        );
    }

    function getArticleReadMore() {
        global $db;
        $start_week_day = date('Y-m-d', get_week_start());
        $end_week_day = date('Y-m-d', get_week_end());
        $sql = "SELECT title,link FROM article WHERE 1 AND status='yes' AND create_date>'$start_week_day' AND create_date<'$end_week_day' ORDER BY view_count DESC LIMIT 1,10";
        $rs = $db->query($sql);
        $totalRows = $db->num_rows($rs);
        if ($totalRows < 5) {
            $sql = "SELECT title,link FROM article WHERE 1 AND status='yes' ORDER BY view_count DESC LIMIT 1,10";
            $rs = $db->query($sql);
            $totalRows = $db->num_rows($rs);
        }
        return array(
            "rs" => $rs,
            "totalRows" => $totalRows
        );
    }

    function hotProjects($cat_id = 8, $limit = 4) {
        global $db;
        $arrCatId = $this->get_cat_id_to_arr($cat_id);
        $str_cat_id = '';
        for ($i = 0; $i < count($arrCatId); $i++) {
            if ($i == (count($arrCatId) - 1)) {
                $str_cat_id .= $arrCatId[$i];
            } else {
                $str_cat_id .= $arrCatId[$i] . ',';
            }
        }
        if ($str_cat_id != '') {
            $sql = "SELECT title,image,link FROM article WHERE cat_id IN ($str_cat_id) AND status='yes' ORDER BY front_page_date DESC LIMIT $limit";
            $rs = $db->query($sql);
            return $rs;
        }
    }

    function getGoldPrice() {
        global $db;
        $sql = "SELECT * FROM foreign_exchange WHERE type='gold' ORDER BY id";
        $rs = $db->query($sql);
        return $rs;
    }

    function getExchange() {
        global $db;
        $sql = "SELECT * FROM foreign_exchange WHERE type='exchange' ORDER BY id";
        $rs = $db->query($sql);
        return $rs;
    }

    // Nhan ban tin email hang tuan
    function isValidEmail($email) {
        return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
    }

    function addNewsletter() {
        if (isset($_POST['email'])) {
            global $db;

            $email = $_POST['email'];

            if ($this->isValidEmail($email)) {
                $checkExist = $this->checkNewsletterExist($email);
                if ($checkExist == 'ok') {
                    $sql = "INSERT INTO newsletter (email) VALUES ('$email');";
                    $db->query($sql);
                    //return 'ok';
                } else {
                    //return 'exist';
                }
                header("Location: /dang-ky-nhan-thu-thanh-cong");
            } else {
                header("Location: /dang-ky-nhan-thu-khong-thanh-cong");
                //return 'email_invalid';
            }
        }
    }

    function checkNewsletterExist($email) {
        global $db;
        $sql = "SELECT id,email FROM newsletter WHERE email='$email'";
        $rs = $db->query_first($sql);
        if (intval($rs['id']) > 0) {
            return 'die';
        } else {
            return 'ok';
        }
    }

}

?>
